package leecode.二分法;

public class 有效的完全平方数 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        boolean perfectSquare = solution.isPerfectSquare(2147483647);
        System.out.println(perfectSquare);

    }

    static  class Solution{
        public boolean isPerfectSquare(int num) {
            int left=1,right=num;
            while(left<=right){
                int mid=left+(right-left)/2;
                if (num%mid==0&&mid==num/mid){      //使用num%mid保证整除,在通过mid==num/mid防止mid*mid数据溢出.
                    return true;
                }else if (mid>num/mid){
                    right=mid-1;
                }else {
                    left = mid + 1;
                }
            }
            return false;
        }
    }
}
